
class Lottery extends eui.Component {
	private bg: eui.Image;
	private ip: eui.Image;
	private warpCir: eui.Image;
	private inCir: eui.Image;
	private deg: number;
	private duration: number;
	private current: eui.Image;
	public lotteryBox: eui.Group;
	public wishBox: eui.Group;
	private ip2: eui.Image;

    public static NUM:number = 12;
	
	constructor() {
		super();
		this.skinName = "LotterySkin";
		this.fadeInAni();
		this.ipAni();
		this.initData();
		this.initGame();
	}

	// 初始化数据
	private initData(): void {
		this.deg = 0;
		this.duration = 0;
	}

	// 渐入效果
	private fadeInAni(): void {
		egret.Tween.get(this.bg).to({ alpha: 1 }, 500, egret.Ease.circIn);
		this.lotteryBox.visible = true;
		egret.Tween.get(this.lotteryBox).to({ alpha: 1 }, 500, egret.Ease.sineIn);	
	}

	// ip动画
	private ipAni(): void {

		this.ip.anchorOffsetY = -140;
		egret.Tween.get(this.ip).wait(500).to({ alpha: 1, anchorOffsetY: 0 }, 500, egret.Ease.backOut);
	}

	// 霓虹灯动画
	private lightAni(): void {

		egret.Tween.get(this.warpCir, { loop: true })
			.set({ source: RES.getRes('wrapcircle_bg2_png') })
			.wait(300)
			.set({ source: RES.getRes('wrapcircle_bg_png') })
			.wait(300);
	}
	
    // 监听按钮事件
    private initGame(): void {
        this.current.touchEnabled = true;
        this.current.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.pressDo.onTouchStart,this);
        this.current.addEventListener(egret.TouchEvent.TOUCH_END,this.pressDo.onTouchEnd,this);
    }

	// 按钮动画
	private pressDo: any = {
		onTouchStart: () => {
			this.current.alpha = 0.8;
			this.current.scaleX = this.current.scaleY = 0.95;
		},
		onTouchEnd: () => {
			this.current.alpha = 1;
			this.current.scaleX = this.current.scaleY = 1;
			this.getGift(); // 获取中奖结果
		}
	}
	
    // 通信部分逻辑 获取中奖结果
    private getGift(): void {
        // 测试
        this.onPostComplete();
    }

    private onPostComplete(event?: egret.Event): void {

        // 测试
        let res = JSON.parse('{ "status": 1, "msg": "奖品5", "prize_id": 1, "prize_name": "iphone7" }');

        if(res.status == 1) {
            //目标
            let prize_id = Math.floor(Math.random() * Lottery.NUM); // res.prize_id
            prize_id = Math.floor(Math.random()*Lottery.NUM);
            this.deg = Math.ceil(Math.random() * 4 + 1) * 360 - prize_id * 360 / Lottery.NUM; // 最少 720 -330 最多 5 * 360
            //持续转动时间
            this.duration = this.calcDuration(this.deg);
            console.log("prize_id：",prize_id,"转动角度：",this.deg,"转动时间：",this.duration);
            return this.startTurn(() => { //this.weiUI.showAlert(res.prize_name),this.lotteryDone() 
                }); // 启动转盘
        }
    }
    
    // 根据角度计算持续时间 deg -> duration
    private calcDuration(deg: number): number {
        return deg * 10;
    }

	// 抽奖动画
	private startTurn(cb: Function): void {
		this.lightAni();
		this.current.touchEnabled = false;
		egret.Tween.get(this.inCir)
		//	.to({ rotation: this.inCir.rotation - 20 }, 500, egret.Ease.sineIn).call(() => {
	.to({ rotation: this.deg }, /*this.duration*/ 5000, egret.Ease.sineIn).call(() => {
				this.current.touchEnabled = true;
//				cb(); // 回调
			});
	}

	private onPostIOError(event: egret.IOErrorEvent): void {
		console.log("post error : " + event);
	}

	private onPostProgress(event: egret.ProgressEvent): void {
		//console.log("post progress : " + Math.floor(100 * event.bytesLoaded / event.bytesTotal) + "%");
	}

	protected childrenCreated(): void {
		super.childrenCreated();
	}
}
